﻿<cfsilent>
	<cfscript>
		
		jsonAdvice = getProperty("serviceFactory").getBean("jsonAdvice");
		
		result = structNew();
		
		bookArray = arrayNew(1);
		pubArray = arrayNew(1);
		
		if ( event.isArgDefined('Keywords') AND len(event.getArg('Keywords')) ) {

			sql = "	SELECT
						b.bid,b.book_name,b.book_edition,b.book_authors,
						p.pid,p.publisher,b.book_prise
					FROM
						t_book b INNER JOIN t_publisher p ON b.pid = p.pid
					WHERE
						b.book_name LIKE :keyword 
						OR
						b.book_isbn LIKE :keyword 
					ORDER BY
						b.book_name";
			
			queryObj = new Query(datasource = application.dnsSlave, maxRows = 30);
			queryObj.addParam(name = "keyword", value = '%'&event.getArg('Keywords')&'%', cfsqltype = "cf_sql_varchar");
			rs_book = queryObj.execute(sql = sql).getResult();

			for( a = 1; a le rs_book.recordCount; a++ ){
				
				if ( not arrayFind(pubArray, [rs_book["pid"][a], rs_book["publisher"][a]]) ) {
					arrayAppend(pubArray, [rs_book["pid"][a], rs_book["publisher"][a]]);
				}
				
				arrayAppend(bookArray,[rs_book["bid"][a],rs_book["book_name"][a],rs_book["book_edition"][a],rs_book["book_authors"][a],rs_book["pid"][a],rs_book["publisher"][a],rs_book["book_prise"][a]]);
				
			}

		}
		
		structInsert(result, "book", bookArray);
		structInsert(result, "publisher", pubArray);
		
	</cfscript>
</cfsilent><cfoutput>#jsonAdvice.encode(data: result, stringNumbers:true)#</cfoutput>